﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sispe.Entities;
using Sispe.BussinesLogic;
using Microsoft.Practices.Unity;

namespace Sispe.UI
{
    public partial class FrmDerechoHabiente : Form
    {
        [Dependency]
        public IDerechoHabienteBL derechoBL { get; set; }

        [Dependency]
        public IPersonaBL personaBL { get; set; }

        public FrmDerechoHabiente()
        {
            InitializeComponent();
            this.BuildUp();
        }

        bool isExtend;

        public void Extend()
        {
            isExtend = true;
        }

        private void txtDescripcion_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                LoadDatA();
            }
        }

        private void txtDescripcion_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            LoadDatA();
        }

        private void FrmDerechoHabiente_Load(object sender, EventArgs e)
        {
            dERECHO_HABIENTEBindingSource.DataSource = derechoBL.GetAllFromDerechoHabientes();   
        }

        private void BtnTraspaso_Click(object sender, EventArgs e)
        {
            DERECHO_HABIENTE currentDerechoHabiente = dERECHO_HABIENTEBindingSource.Current as DERECHO_HABIENTE;

            if (currentDerechoHabiente != null)
            {
                FrmTraspasoDerechoHabiente frmtraspasoderechohabiente = new FrmTraspasoDerechoHabiente();
                frmtraspasoderechohabiente.CurrentDerechoHabiente = currentDerechoHabiente;
                frmtraspasoderechohabiente.Editar();
                frmtraspasoderechohabiente.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("Debe seleccionar un Derecho Habiente");
            }           

            LoadDatA();


        }

        private void rbActivo_CheckedChanged(object sender, EventArgs e)
        {
            
        }

        private void rbActivo_Click(object sender, EventArgs e)
        {
            
        }

        private void cbActivo_CheckedChanged(object sender, EventArgs e)
        {
            LoadDatA();
        }

        private void rbNombres_CheckedChanged(object sender, EventArgs e)
        {
            txtDescripcion.Enabled = true;
            LoadDatA();
        }

        private void rbVinculoFam_CheckedChanged(object sender, EventArgs e)
        {
            txtDescripcion.Enabled = true;
            LoadDatA();
        }

        private void rbDocumento_CheckedChanged(object sender, EventArgs e)
        {
            txtDescripcion.Enabled = true;
            LoadDatA();
        }

        private void rbConstancia_CheckedChanged(object sender, EventArgs e)
        {
            txtDescripcion.Text = "";
            txtDescripcion.Enabled = false;
            LoadDatA();
        }

        private void LoadDatA()
        {
            if (cbActivo.Checked)
            {
                if (cbActivo.Checked && rbNombres.Checked)
                {
                    dERECHO_HABIENTEBindingSource.DataSource = derechoBL.GetFromDerechoHabienteByActico(txtDescripcion.Text, "", "", cbActivo.Checked, false);
                }
                if (cbActivo.Checked && rbVinculoFam.Checked)
                {
                    dERECHO_HABIENTEBindingSource.DataSource = derechoBL.GetFromDerechoHabienteByActico("", txtDescripcion.Text, "", cbActivo.Checked, false);
                }
                if (cbActivo.Checked && rbDocumento.Checked)
                {
                    dERECHO_HABIENTEBindingSource.DataSource = derechoBL.GetFromDerechoHabienteByActico("", "", txtDescripcion.Text, cbActivo.Checked, false);
                }
                if (cbActivo.Checked && rbConstancia.Checked)
                {
                    dERECHO_HABIENTEBindingSource.DataSource = derechoBL.GetFromDerechoHabienteByActico("", "", txtDescripcion.Text, cbActivo.Checked, rbConstancia.Checked);
                }
            }
            else
            {
                if (cbActivo.Checked == false && rbNombres.Checked)
                {
                    dERECHO_HABIENTEBindingSource.DataSource = derechoBL.GetFromDerechoHabienteByActico(txtDescripcion.Text, "", "", false, false);
                }
                if (cbActivo.Checked == false && rbVinculoFam.Checked)
                {
                    dERECHO_HABIENTEBindingSource.DataSource = derechoBL.GetFromDerechoHabienteByActico("", txtDescripcion.Text, "", false, false);
                }
                if (cbActivo.Checked == false && rbDocumento.Checked)
                {
                    dERECHO_HABIENTEBindingSource.DataSource = derechoBL.GetFromDerechoHabienteByActico("", "", txtDescripcion.Text, false, false);
                }
                if (cbActivo.Checked == false && rbConstancia.Checked)
                {
                    dERECHO_HABIENTEBindingSource.DataSource = derechoBL.GetFromDerechoHabienteByActico("", "", txtDescripcion.Text, false, rbConstancia.Checked);
                }
            }
        }

        private void dERECHO_HABIENTERadGridView_CurrentCellChanged(object sender, Telerik.WinControls.UI.CurrentCellChangedEventArgs e)
        {
            var derecho = dERECHO_HABIENTEBindingSource.Current as DERECHO_HABIENTE;

            if (derecho != null)
            {
                var persona = personaBL.GetFromPersonaById(derecho.IdTrabajador);
                LTrabajador.Text = persona.NombreCompleto;
            }
        }

        private void btnDetalle_Click(object sender, EventArgs e)
        {
            var _derechohabiente = dERECHO_HABIENTEBindingSource.Current as DERECHO_HABIENTE;
            if (_derechohabiente != null)
            {
                FrmMttoDerechoHabiente frm = new FrmMttoDerechoHabiente();
                frm.VerDetalle(_derechohabiente.IdDerechoHabiente);
                frm.ShowDialog();
            }
            else
            {
                MessageBox.Show("Seleccione un Item","Mensaje",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }

    }
}
